package ink.chow.jdbc.sharding.conf;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

public class StringModShardingAlgorithm implements PreciseShardingAlgorithm<String>{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
		logger.info("sharding : {}", availableTargetNames);
		for(String name : availableTargetNames) {
			if (name.endsWith(shardingValue.getValue().hashCode() % 2+"")) {
				return name;
			}
		}
		throw new UnsupportedOperationException();
	}

}
